<template>
   <el-dialog title="下发选项" :visible.sync="visible"
   :width="'500px'"
   :modal-append-to-body="true"
   :append-to-body="true"
   @open="dialogOpen"
   @close="closeEvent"
   :lock-scroll="true"
   >
   <el-form v-model="model" :label-width="'150px'">
      <el-form-item label="境内居住地派出所:" v-if="mode === 'single' && model.jnValue">
         <el-checkbox v-model="model.jn" :label="model.jnName" :name="model.jnValue"></el-checkbox>
      </el-form-item>
      <el-form-item label="境内工作地派出所:" v-if="mode === 'single' && model.gzdValue">
         <el-checkbox v-model="model.gzd" :label="model.gzdName" :name="model.gzdValue"></el-checkbox>
      </el-form-item>
      <el-form-item label="反馈截止时间:">
           <el-date-picker
                     size="mini"
                      v-model="model.limitTime"
                      type="datetime"
                      :default-value="defaultTime"
                      placeholder="选择日期时间">
            </el-date-picker>
      </el-form-item>
   </el-form>
   <el-button slot="footer" type="primary" size="small" @click="confirm">确认下发</el-button>
     </el-dialog>
</template>
<script>
import date from '@/utils/date';
export default {
  props: {
    visible: {
      type: Boolean
    },
    mode: {
      type: String,
      default: 'batch' // batch, single
    },
    data: {
      type: Object,
      default() {
        return {};
      }
    },
    afterChoose: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      defaultTime: new Date(),
      model: {
        gzd: true,
        jn: true,
        jnName: '',
        jnValue: '',
        gzdName: '',
        gzdValue: '',
        limitTime: null
      }
    };
  },
  methods: {
    dialogOpen() {
      this.reset();
      this.$nextTick(r => {
        // 如果单独下发则需选择派出所
        if (this.data && this.mode === 'single') {
          this.model.jnName = this.data.caddrpolicecode;
          this.model.jnValue = this.data.addrpolicecode;
          this.model.gzdName = this.data.cworkpolice;
          this.model.gzdValue = this.data.workpolice;
        }
      });
    },
    reset() {
      this.model = {
        gzd: true,
        jn: true,
        jnName: '',
        jnValue: '',
        gzdName: '',
        gzdValue: '',
        limitTime: new Date()
      };
    },
    confirm() {
      let postData = {};
      if (!this.model.jn && !this.model.gzd) {
        this.$message({
          type: 'warning',
          message: '至少选择一间派出所下发'
        });
        return;
      }
      if (!this.model.limitTime) {
        this.$message({
          type: 'warning',
          message: '请选择反馈截止时间'
        });
        return;
      }
      if (this.mode === 'single') {
        if(this.data.jnValue) {
          postData.sendAddr = this.model.jn ? 1 : 0;
        }
        if(this.data.gzdValue) {
          postData.sendWork = this.model.gzd ? 1 : 0;
        }
      }
      postData.limitTime = date(this.model.limitTime, 'yyyy-MM-dd hh:mm:ss');
      this.afterChoose.resolve(postData);
      this.closeEvent(true);
    },
    closeEvent(success) {
      if (!success) {
        this.afterChoose.resolve();
      }
      this.$emit('close');
    }
  }
};
</script>